package Dao.Imp;

import Dao.RoomMasterDao;
import Request.DispartIdRequest;
import Request.LiveRequest;
import modal.RoomMaster;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * Created by tan on 2017/8/24.
 * 14:39
 */
@Repository
@Transactional
public class RoomMasterDaoImp implements RoomMasterDao {
    @Autowired
    SessionFactory sessionFactory;
    public RoomMaster getRoomMasterByRoomid(String roomid) {
        Session session=sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("select * from room_master where roomid = ? ");
            Query query = session.createSQLQuery(sql.toString()).setString(0,roomid);
            query.setResultTransformer(Transformers.aliasToBean(RoomMaster.class));
            return (RoomMaster)query.uniqueResult();
        }catch (Exception e)
        {
            System.out.println("RoomMasterDaoImp\n获取实例失败");
            e.printStackTrace();
        }
        return  null;
    }

    public String updateRoomMaster(RoomMaster master) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("update room_master set status=? ,roomname =? ,point =?,description=?,picturesrc=? where roomid=?");
            Query query=session.createSQLQuery(sql.toString());
            query.setInteger(0,master.getStatus());
            query.setString(1,master.getRoomname());
            query.setInteger(2,master.getPoint());
            query.setString(3,master.getDescription());
            query.setString(4,master.getPicturesrc());
            query.setString(5,master.getRoomid());
            if(query.executeUpdate()==1)
            {
                return "success";
            }
        }catch (Exception e)
        {
            System.out.println("RoomMaterDaoImp\n Method:updateRoomMaster");
            e.printStackTrace();
        }
        return "error";
    }

    public RoomMaster getRoomMasterByName(String name) {
        Session session=sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("select * from room_master where username = ? ");
            Query query = session.createSQLQuery(sql.toString()).setString(0,name);
            query.setResultTransformer(Transformers.aliasToBean(RoomMaster.class));
            return (RoomMaster)query.uniqueResult();
        }catch (Exception e)
        {
            System.out.println("RoomMasterDaoImp\n获取实例失败");
            e.printStackTrace();
        }
        return  null;
    }

    public String openLive(LiveRequest liveRequest) {
        Session session=sessionFactory.getCurrentSession();

        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("update room_master set status=? where roomid=?");
            Query query =session.createSQLQuery(sql.toString()).setString(0,liveRequest.getStatus()).setString(1,liveRequest.getId());
            if(query.executeUpdate()==1)
            {
                return "success";
            }
        }catch (Exception e)
        {
            System.out.println("直播失败");
            e.printStackTrace();
        }
        return "error";
    }

    public String addRoommaster(DispartIdRequest request) {
        Session session=sessionFactory.getCurrentSession();

        try
        {
            String key= UUID.randomUUID().toString().replace("-","");
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT room_master (username,roomid,roomkey) VALUES (?,?,?)");
            Query query = session.createSQLQuery(sql.toString()).setString(0,request.getUsername()).
                    setString(1,request.getRoomid())
                    .setString(2,key);
            if(query.executeUpdate()==1)
            {
                return "success";
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("分配房间号失败");
        }
        return "error";
    }

    public List getAllRoom() {
        Session session=sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("select * from room_master where status='1'");
            Query query = session.createSQLQuery(sql.toString());
            query.setResultTransformer(Transformers.aliasToBean(RoomMaster.class));
            return query.list();
        }catch (Exception e){
            System.out.print("获取失败");
            e.printStackTrace();
        }
        return null;
    }

}
